package com.kystar.muxplayer.ui;

import android.content.Context;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.TextView;
import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.model.Version;
import com.kystar.muxplayer.protocol.code.set.CS08_Language;
import com.kystar.muxplayer.selfview.MyKeyBoard;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class SystemGroupView extends GroupView {
    MyKeyBoard mKeyBoard;

    public SystemGroupView(Context context,MyKeyBoard myKeyBoard) {
        super(context, "系统设置");
        mKeyBoard = myKeyBoard;
        addSubGroup("多级同步", R.layout.layout_main_settings_system_sync, false);
        addSubGroup("技术支持", R.layout.layout_main_settings_system_support, false);
        addSubGroup("语言设置", R.layout.layout_main_settings_system_language, false);
        addSubGroup("监视设置", R.layout.layout_main_settings_system_monitor, true);

        setupSyncView();
        setupSupportView();
        setupLanguageView();
        setupMonitorView();
    }

    private void setupLanguageView() {
        ViewList views = new ViewList(findViewById(R.id.system_language),true);
        views.select(BaseApp.getSettings().getVersion().getLanguage(),false);
        views.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag = Helper.getTagInt(v,0);
                BaseApp.send(CS08_Language.create(tag)).subscribe();
            }
        });
    }

    private void setupSyncView() {
        View closeView = findViewById(R.id.system_sync_close);
        View customView = findViewById(R.id.system_sync_custom);
        ViewList syncHead = ViewList.buttons(findViewById(R.id.system_sync_head), true);
        syncHead.setOnClickListener(v->{
            if("0".equals(v.getTag().toString())) {
                Helper.showView(Helper.getChildView(syncHead.get(0), 1));
                Helper.showView(closeView);
                Helper.hideView(Helper.getChildView(syncHead.get(1), 1));
                Helper.hideView(customView);
            } else {
                Helper.hideView(Helper.getChildView(syncHead.get(0), 1));
                Helper.hideView(closeView);
                Helper.showView(Helper.getChildView(syncHead.get(1), 1));
                Helper.showView(customView);
            }
        });
//        setupConfirm(closeView, "是否关闭多机同步？",
//                ()-> connection.run(p-> p.setSyncSource(0, false)).post(),
//                ()-> syncHead.get(1).performClick());

        ViewList syncSource = new ViewList();
        Helper.each(Helper.getChildren(customView),
                view -> syncSource.addAll(Helper.getChildren(view)));
        syncSource.setOnClickListener(v -> {
            int source = Helper.getTagInt(v, 0);
           // connection.run(p->p.setSyncSource(source, true)).post();
        });
        syncHead.get(1).performClick();
    }

    private static final String VERSION_FMT = "北京凯视达科技\n" +
            "网址：www.kystar.com.cn\n" +
            "热线：400-0000-267\n" +
            "型号：K1\n" +
            "IP地址：%s\n" +
            "版本信息：ARM：%s\n" +
            "　　　　　FPGA：%s\n";

    private void setupSupportView() {
        Version version = BaseApp.getSettings().getVersion();
        String support = String.format(VERSION_FMT, version.getIp(), version.getArmVersion(), version.getFpgaVersion());
        ((TextView)findViewById(R.id.system_support_text)).setText(support);
    }

    private void setupMonitorView() {
        View content = findViewById(R.id.system_monitor_content);
        View applyView = findViewById(R.id.system_take_mode_content);
        ViewList head = ViewList.buttons(findViewById(R.id.system_monitor_head), true);
        ViewList monitorMode = ViewList.buttons(content, true);
        ViewList takeMode = new ViewList();
        Helper.eachChild(applyView, view -> {
            if(view.getTag() != null) {
                takeMode.add(view);
            }
        });

        head.setOnClickListener(v -> {
            if (Helper.getTagInt(v, 0) == 1) {
                Helper.hideView(content);
                Helper.showView(applyView);
            } else {
                Helper.showView(content);
                Helper.hideView(applyView);
            }
        });
        monitorMode.setOnClickListener(v -> {
            int mode = Helper.getTagInt(v, 0);
//            connection.run(p -> p.setMonitorMode(mode)).onOK(()->{
//                settings.monitorMode = mode;
//                muxModel.pvwSubject.publish(settings.pvw,
//                        Event.EventSource.Settings, Event.EventType.MonitorMode);
//            }).post();
        });

        takeMode.setOnClickListener(v -> {
            int mode = Helper.getTagInt(v, 0);
           // connection.run(p -> p.setTakeMode(mode)).post();
        });

        head.get(1).performClick();
    }

}
